# Copyright (C) 2007 Richard Boulton
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
r"""tables.py: Tables used by the feedreader.

"""

import datetime
from storm.locals import *
from utils import *

class AppInfo(Storm):
    __storm_table__ = "appinfo"

    name = Unicode(primary=True)
    value = Unicode()

    def __init__(self, name, value):
        self.name = tounicode(name)
        self.value = tounicode(value)

class FeedEntry(Storm):
    __storm_table__ = "feedentry"

    id = Int(primary=True)
    title = Unicode()
    text = Unicode()

class Feed(Storm):
    __storm_table__ = "feed"

    id = Int(primary=True)
    name = Unicode()
    url = Unicode()
    language = Unicode()

    update_interval = Int()  # in seconds
    next_update_due = DateTime()

    def __init__(self, name, url, language):
        self.name = tounicode(name)
        self.url = tounicode(url)
        self.language = tounicode(language)
        self.update_interval = 0 # 0 means, use default interval
        self.next_update_due = datetime.datetime.utcnow()


def create_all(store):
    """Create all the tables.

    """
    store.execute("""CREATE TABLE appinfo 
                  (name VARCHAR PRIMARY KEY,
                   value VARCHAR)""")

    store.execute("""CREATE TABLE feedentry 
                  (id INTEGER PRIMARY KEY,
                   title VARCHAR,
                   text VARCHAR)""")

    store.execute("""CREATE TABLE feed 
                  (id INTEGER PRIMARY KEY,
                   name VARCHAR,
                   url VARCHAR,
                   language VARCHAR,
                   update_interval INTEGER,
                   next_update_due DATETIME)""")
